"use strict";
cc._RF.push(module, '877071dz1JBfo7aHiG3/0fs', 'BattleUI');
// script/Game/Controller/Screen/BattleUI.ts

"use strict";
var __extends = (this && this.__extends) || (function () {
    var extendStatics = function (d, b) {
        extendStatics = Object.setPrototypeOf ||
            ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
            function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
        return extendStatics(d, b);
    };
    return function (d, b) {
        extendStatics(d, b);
        function __() { this.constructor = d; }
        d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
    };
})();
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
    var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
    if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
    else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
    return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
var __generator = (this && this.__generator) || function (thisArg, body) {
    var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
    return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
    function verb(n) { return function (v) { return step([n, v]); }; }
    function step(op) {
        if (f) throw new TypeError("Generator is already executing.");
        while (_) try {
            if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
            if (y = 0, t) op = [op[0] & 2, t.value];
            switch (op[0]) {
                case 0: case 1: t = op; break;
                case 4: _.label++; return { value: op[1], done: false };
                case 5: _.label++; y = op[1]; op = [0]; continue;
                case 7: op = _.ops.pop(); _.trys.pop(); continue;
                default:
                    if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
                    if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
                    if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
                    if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
                    if (t[2]) _.ops.pop();
                    _.trys.pop(); continue;
            }
            op = body.call(thisArg, _);
        } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
        if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
    }
};
Object.defineProperty(exports, "__esModule", { value: true });
var GridsLib_1 = require("../../Libs/GridsLib");
var CardLib_1 = require("../../Libs/CardLib");
var Untils_1 = require("../../Libs/Untils");
var BaseUI_1 = require("../../../Core/BaseUI");
var _a = cc._decorator, ccclass = _a.ccclass, property = _a.property;
var BattleUI = /** @class */ (function (_super) {
    __extends(BattleUI, _super);
    function BattleUI() {
        var _this = _super !== null && _super.apply(this, arguments) || this;
        //格子分布父节点
        _this.gridNode = null;
        _this.bossCounter = null;
        _this.coinCounter = null;
        _this.isMoving = true;
        _this.defaultBossStep = 10;
        _this.moveStep = 0;
        _this.coinTotal = 0;
        return _this;
        // update (dt) {}
    }
    BattleUI.prototype.onShow = function (data) {
        return __awaiter(this, void 0, Promise, function () {
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        this.gridsLib = new GridsLib_1.GridsLib(this.gridNode);
                        return [4 /*yield*/, this.gridsLib.generateGrids(3, 3)];
                    case 1:
                        _a.sent();
                        this.gridsLib.registerClickEvent(this.clickNode, this);
                        return [4 /*yield*/, this.gridsLib.setGridsCard([
                                { type: GridsLib_1.CardType.Monster, id: 9, value: 2 },
                                { type: GridsLib_1.CardType.Health, id: 1, value: 2 },
                                { type: GridsLib_1.CardType.Trap, id: 1, value: 1 },
                                { type: GridsLib_1.CardType.Treasure, id: 1, value: 3 },
                                { type: GridsLib_1.CardType.Hero, id: 2, value: 10, maxValue: 10 },
                                { type: GridsLib_1.CardType.Health, id: 1, value: 3 },
                                { type: GridsLib_1.CardType.Trap, id: 1, value: 1 },
                                { type: GridsLib_1.CardType.Monster, id: 3, value: 1 },
                                { type: GridsLib_1.CardType.Health, id: 1, value: 1 },
                                { type: GridsLib_1.CardType.Trap, id: 1, value: 1 },
                                { type: GridsLib_1.CardType.Monster, id: 3, value: 1 },
                                { type: GridsLib_1.CardType.Health, id: 1, value: 1 },
                            ])];
                    case 2:
                        _a.sent();
                        //boss步数初始
                        this.setBossStep(this.defaultBossStep);
                        //金币数
                        this.setCoinCount(0);
                        this.isMoving = false;
                        return [2 /*return*/];
                }
            });
        });
    };
    BattleUI.prototype.onHide = function () {
        return;
    };
    BattleUI.prototype.clickNode = function (event) {
        return __awaiter(this, void 0, void 0, function () {
            var targetGrid, heroInfo, offset, direction;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        if (this.isMoving) {
                            return [2 /*return*/];
                        }
                        targetGrid = this.gridsLib.getGridInfoByNode(event.target);
                        if (targetGrid.cardInfo.type == GridsLib_1.CardType.Hero) {
                            return [2 /*return*/];
                        }
                        heroInfo = this.gridsLib.getGridInfoByCardType(GridsLib_1.CardType.Hero)[0];
                        offset = heroInfo.vec2.sub(targetGrid.vec2);
                        if (offset.x == 0 && offset.y == 1) {
                            direction = GridsLib_1.Direction.Left;
                        }
                        else if (offset.x == 0 && offset.y == -1) {
                            direction = GridsLib_1.Direction.Right;
                        }
                        else if (offset.x == 1 && offset.y == 0) {
                            direction = GridsLib_1.Direction.UP;
                        }
                        else if (offset.x == -1 && offset.y == 0) {
                            direction = GridsLib_1.Direction.Down;
                        }
                        else {
                            return [2 /*return*/];
                        }
                        if (!this.canMove(heroInfo, targetGrid)) return [3 /*break*/, 2];
                        //先移动卡牌
                        return [4 /*yield*/, this.move(direction, heroInfo, targetGrid)];
                    case 1:
                        //先移动卡牌
                        _a.sent();
                        _a.label = 2;
                    case 2: 
                    //触发效果
                    return [4 /*yield*/, this.triggerEffect(heroInfo, targetGrid)];
                    case 3:
                        //触发效果
                        _a.sent();
                        this.isMoving = false;
                        return [2 /*return*/];
                }
            });
        });
    };
    BattleUI.prototype.isDead = function (heroInfo, targetGrid) {
        return heroInfo.node.getComponent(CardLib_1.default).hp <= targetGrid.cardInfo.value;
    };
    BattleUI.prototype.canMove = function (heroInfo, targetGrid) {
        switch (targetGrid.cardInfo.type) {
            case GridsLib_1.CardType.Treasure:
                return false;
            case GridsLib_1.CardType.Boss:
            case GridsLib_1.CardType.Monster:
            case GridsLib_1.CardType.Trap:
                return this.isDead(heroInfo, targetGrid) == false;
            default:
                return true;
        }
    };
    BattleUI.prototype.triggerEffect = function (heroInfo, targetGrid) {
        return __awaiter(this, void 0, void 0, function () {
            var _a;
            return __generator(this, function (_b) {
                switch (_b.label) {
                    case 0:
                        _a = targetGrid.cardInfo.type;
                        switch (_a) {
                            case GridsLib_1.CardType.Treasure: return [3 /*break*/, 1];
                            case GridsLib_1.CardType.Shield: return [3 /*break*/, 3];
                            case GridsLib_1.CardType.Boss: return [3 /*break*/, 4];
                            case GridsLib_1.CardType.Monster: return [3 /*break*/, 5];
                            case GridsLib_1.CardType.Trap: return [3 /*break*/, 5];
                            case GridsLib_1.CardType.Health: return [3 /*break*/, 6];
                            case GridsLib_1.CardType.Coin: return [3 /*break*/, 7];
                            case GridsLib_1.CardType.Skill: return [3 /*break*/, 8];
                        }
                        return [3 /*break*/, 10];
                    case 1: return [4 /*yield*/, this.gridsLib.setCardOnGrid(this.getTreasureRandCardInfo(targetGrid.cardInfo.value), targetGrid)];
                    case 2:
                        _b.sent();
                        return [3 /*break*/, 11];
                    case 3:
                        heroInfo.node.getComponent(CardLib_1.default).addDefenseValue(targetGrid.cardInfo.value);
                        return [3 /*break*/, 11];
                    case 4:
                        this.moveStep = 0; //恢复计数
                        this.setBossStep(this.defaultBossStep);
                        _b.label = 5;
                    case 5:
                        if (this.isDead(heroInfo, targetGrid)) {
                            //死亡动画
                            return [3 /*break*/, 11];
                        }
                        //扣血
                        heroInfo.node.getComponent(CardLib_1.default).addHp(-targetGrid.cardInfo.value);
                        return [3 /*break*/, 11];
                    case 6:
                        //加血
                        heroInfo.node.getComponent(CardLib_1.default).addHp(targetGrid.cardInfo.value);
                        return [3 /*break*/, 11];
                    case 7:
                        this.coinTotal += targetGrid.cardInfo.value;
                        this.setCoinCount(this.coinTotal);
                        return [3 /*break*/, 11];
                    case 8: return [4 /*yield*/, this.triggerSkillEffect(targetGrid)];
                    case 9:
                        _b.sent();
                        return [3 /*break*/, 11];
                    case 10: return [2 /*return*/];
                    case 11: return [2 /*return*/];
                }
            });
        });
    };
    BattleUI.prototype.triggerSkillEffect = function (skillGrid) {
        return __awaiter(this, void 0, void 0, function () {
            var heroInfo, _i, _a, direction, targetGrid, _b, cardInfo;
            return __generator(this, function (_c) {
                switch (_c.label) {
                    case 0:
                        heroInfo = this.gridsLib.getGridInfoByCardType(GridsLib_1.CardType.Hero)[0];
                        _i = 0, _a = [GridsLib_1.Direction.UP, GridsLib_1.Direction.Down, GridsLib_1.Direction.Left, GridsLib_1.Direction.Right];
                        _c.label = 1;
                    case 1:
                        if (!(_i < _a.length)) return [3 /*break*/, 8];
                        direction = _a[_i];
                        targetGrid = this.gridsLib.getGridInfoByDirection(heroInfo.index, Number(direction), GridsLib_1.DirectionMode.Forward);
                        if (!targetGrid) {
                            return [3 /*break*/, 7];
                        }
                        _b = targetGrid.cardInfo.type;
                        switch (_b) {
                            case GridsLib_1.CardType.Shield: return [3 /*break*/, 2];
                            case GridsLib_1.CardType.Boss: return [3 /*break*/, 2];
                            case GridsLib_1.CardType.Monster: return [3 /*break*/, 2];
                            case GridsLib_1.CardType.Trap: return [3 /*break*/, 2];
                            case GridsLib_1.CardType.Health: return [3 /*break*/, 2];
                            case GridsLib_1.CardType.Coin: return [3 /*break*/, 6];
                            case GridsLib_1.CardType.Skill: return [3 /*break*/, 6];
                        }
                        return [3 /*break*/, 7];
                    case 2:
                        if (!(skillGrid.cardInfo.value >= targetGrid.cardInfo.value)) return [3 /*break*/, 4];
                        if (targetGrid.cardInfo.type == GridsLib_1.CardType.Boss) {
                            this.moveStep = 0; //恢复计数
                            this.setBossStep(this.defaultBossStep);
                        }
                        cardInfo = { type: GridsLib_1.CardType.Coin, id: this.getCardRandId(GridsLib_1.CardType.Coin), value: this.getCardRandValue(GridsLib_1.CardType.Coin) };
                        return [4 /*yield*/, this.gridsLib.setCardOnGrid(cardInfo, targetGrid, true)];
                    case 3:
                        _c.sent();
                        return [3 /*break*/, 5];
                    case 4:
                        targetGrid.node.getComponent(CardLib_1.default).addValue(-skillGrid.cardInfo.value);
                        targetGrid.cardInfo.value -= skillGrid.cardInfo.value;
                        _c.label = 5;
                    case 5: return [3 /*break*/, 7];
                    case 6:
                        targetGrid.node.getComponent(CardLib_1.default).addValue(skillGrid.cardInfo.value);
                        targetGrid.cardInfo.value += skillGrid.cardInfo.value;
                        return [3 /*break*/, 7];
                    case 7:
                        _i++;
                        return [3 /*break*/, 1];
                    case 8: return [2 /*return*/];
                }
            });
        });
    };
    BattleUI.prototype.move = function (direction, heroGrid, targetGrid) {
        return __awaiter(this, void 0, void 0, function () {
            var moveList, followGrids;
            var _this = this;
            return __generator(this, function (_a) {
                switch (_a.label) {
                    case 0:
                        moveList = [targetGrid, heroGrid];
                        followGrids = this.gridsLib.getAllGridsInfoByDirection(heroGrid.index, direction, GridsLib_1.DirectionMode.Behind);
                        //没有背后的节点 先找顺时针方向的
                        if (followGrids.length == 0) {
                            followGrids = this.gridsLib.getAllGridsInfoByDirection(heroGrid.index, direction, GridsLib_1.DirectionMode.Clockwise);
                        }
                        //还没有 找逆时针方向的
                        if (followGrids.length == 0) {
                            followGrids = this.gridsLib.getAllGridsInfoByDirection(heroGrid.index, direction, GridsLib_1.DirectionMode.AntiClockwise);
                        }
                        //开始移动 并补卡
                        return [4 /*yield*/, this.gridsLib.startMove(moveList.concat(followGrids)).then(function (gridInfo) { return __awaiter(_this, void 0, void 0, function () {
                                var cardInfo;
                                return __generator(this, function (_a) {
                                    switch (_a.label) {
                                        case 0:
                                            if (this.moveStep >= 0) {
                                                //下一步出boss
                                                if (this.defaultBossStep - (this.moveStep + 1) <= 0) {
                                                    this.moveStep = -1;
                                                    cardInfo = {
                                                        type: GridsLib_1.CardType.Boss,
                                                        id: 1,
                                                        value: this.getCardRandValue(GridsLib_1.CardType.Boss)
                                                    };
                                                    this.setBossStep("-");
                                                }
                                                else {
                                                    this.moveStep++;
                                                    this.setBossStep(this.defaultBossStep - this.moveStep);
                                                }
                                            }
                                            if (!cardInfo) {
                                                cardInfo = this.getMoveRandCardInfo();
                                            }
                                            return [4 /*yield*/, this.gridsLib.setCardOnGrid(cardInfo, gridInfo)];
                                        case 1:
                                            _a.sent();
                                            return [2 /*return*/];
                                    }
                                });
                            }); })];
                    case 1:
                        //开始移动 并补卡
                        _a.sent();
                        return [2 /*return*/];
                }
            });
        });
    };
    //移动补卡
    BattleUI.prototype.getMoveRandCardInfo = function () {
        //权重数组
        var weightArr = [
            { id: GridsLib_1.CardType.Monster, weight: 150 },
            { id: GridsLib_1.CardType.Trap, weight: 100 },
            { id: GridsLib_1.CardType.Health, weight: 100 },
            { id: GridsLib_1.CardType.Coin, weight: 80 },
            { id: GridsLib_1.CardType.Skill, weight: 100 },
            { id: GridsLib_1.CardType.Shield, weight: 100 },
            { id: GridsLib_1.CardType.Treasure, weight: 120 },
        ];
        var index = Untils_1.randByWeight(weightArr);
        return {
            type: weightArr[index].id,
            id: this.getCardRandId(weightArr[index].id),
            value: this.getCardRandValue(weightArr[index].id)
        };
    };
    //宝箱随机卡
    BattleUI.prototype.getTreasureRandCardInfo = function (value) {
        //权重数组
        var weightArr = [
            { id: GridsLib_1.CardType.Monster, weight: 100 },
            { id: GridsLib_1.CardType.Trap, weight: 100 },
            { id: GridsLib_1.CardType.Health, weight: 200 },
            { id: GridsLib_1.CardType.Coin, weight: 300 },
            { id: GridsLib_1.CardType.Skill, weight: 200 },
            { id: GridsLib_1.CardType.Shield, weight: 200 },
        ];
        var index = Untils_1.randByWeight(weightArr);
        return {
            type: weightArr[index].id,
            id: this.getCardRandId(weightArr[index].id),
            value: value
        };
    };
    //获取卡牌随机id
    BattleUI.prototype.getCardRandId = function (type) {
        var min, max;
        switch (type) {
            case GridsLib_1.CardType.Shield:
                min = 1;
                max = 5;
                break;
            case GridsLib_1.CardType.Monster:
                min = 1;
                max = 9;
                break;
            case GridsLib_1.CardType.Skill:
                min = 1;
                max = 2;
                break;
            default:
                min = 1;
                max = 1;
        }
        return Untils_1.getRandomIntInclusive(min, max);
    };
    //获取随机卡牌值
    BattleUI.prototype.getCardRandValue = function (type) {
        var min, max;
        if (type == GridsLib_1.CardType.Boss) {
            min = 5;
            max = 9;
        }
        else if (type == GridsLib_1.CardType.Trap) {
            min = 1;
            max = 1;
        }
        else {
            min = 1;
            max = 3;
        }
        return Untils_1.getRandomIntInclusive(min, max);
    };
    BattleUI.prototype.setBossStep = function (n) {
        this.bossCounter.getComponent(cc.Label).string = n.toString();
    };
    BattleUI.prototype.setCoinCount = function (n) {
        this.coinCounter.getComponent(cc.Label).string = n.toString();
    };
    __decorate([
        property(cc.Node)
    ], BattleUI.prototype, "gridNode", void 0);
    __decorate([
        property(cc.Node)
    ], BattleUI.prototype, "bossCounter", void 0);
    __decorate([
        property(cc.Node)
    ], BattleUI.prototype, "coinCounter", void 0);
    BattleUI = __decorate([
        ccclass
    ], BattleUI);
    return BattleUI;
}(BaseUI_1.default));

cc._RF.pop();